<?php
include ('config.php');
include 'checkpostandget.php';
function sel_sql($table,$sel_field,$where,$alert=0){
	global $BIAOTOU;
	$sql="select $sel_field from $BIAOTOU$table where $where";
	if($alert==1){
	    echo $sql;
	}
	$query=mysql_query($sql);
	if($query){
		while($row=mysql_fetch_array($query)){
			if(strpos($sel_field,",") or $sel_field=="*"){
			    $arr=$row;
			}
			else{
			    $arr=$row[$sel_field];
			}
		}
	}
	return $arr;	
}

function insert_one_sql($table,$field_arr,$alert=0){
    global $BIAOTOU;
	$field="";
	$values="";
	foreach($field_arr as $k=>$v){
		$field = "`".$k."`,".$field;
		$values = "'".$v."',".$values;
		}
	$field=substr($field,0,strlen($field)-1);
	$values=substr($values,0,strlen($values)-1);
	$sql="insert into $BIAOTOU$table($field) values ($values);";
	/*$fp = fopen('../data/linktech.txt', 'a');
fwrite($fp, $sql . "\n\r");
fclose($fp);*/
	$query=mysql_query($sql);
	if($alert==0){
	    if($query) return 1;
	    else return mysql_error();
	}
	elseif($alert==1){
	    echo $sql;
	}
	elseif($alert==2){
		echo $sql;
	    if($query) return 1;
	    else return mysql_error();
	}

}



$date = date('Ymd');

$link = mysql_connect($dbserver, $dbuser, $dbpass);
mysql_query("set names utf8");
mysql_select_db($dbname);

$order_time = $_GET['yyyymmdd']; //日期
$merchant_id = $_GET['m_id']; //广告主ID
$u_id = $_GET['u_id']?$_GET['u_id']:0; //会员ID

$dduser = sel_sql("user", "ddusername,dengji", "Id='$u_id'");
$ddusername = $dduser['ddusername'];
$dengji = $dduser['dengji'];

$order_code = $_GET['o_cd']; //订货号
$product_code = $_GET['p_cd']; //商品编号
$item_price = $_GET['price']; //商品单价
$item_count = $_GET['it_cnt']; //商品数量
$sales = $item_price * $item_count; //总额
$commission = $_GET['comm']; //返利金额
$stat_desc = '未核对'; //订单状态
$bill_yyyymmdd = ''; //结算日
$fxje = mall_ck_fxje($commission, $dengji); //会员返利金额
$tgje = round($commission * TGBL, 2); //给会员的推广佣金

$txt = "广告主：" . $merchant_id . " | 订货号：" . $order_code . " | 接收时间" . date('Y-m-d H:i:s');
$fp = fopen('../data/linktech.txt', 'a');
fwrite($fp, $txt . "\n\r");
fclose($fp);

//添加交易记录
$id = sel_sql("mall_order", "id", "order_code='$order_code' and product_code='$product_code'"); //用订货号和商品编号验证该交易是否存在于数据库
if ($id == '') { //交易不存在
	$filed_arr = array (
		'order_time' => $order_time,
		'product_code' => $product_code,
		'merchant_id' => $merchant_id,
		'u_id' => $u_id,
		'order_code' => $order_code,
		'item_count' => $item_count,
		'item_price' => $item_price,
		'sales' => $sales,
		'commission' => $commission,
		'stat_desc' => $stat_desc,
		'bill_yyyymmdd' => $bill_yyyymmdd,
		'fxje' => $fxje,
		'ddusername' => $ddusername,
		'lm' => 'linktech'
	);
	insert_one_sql("mall_order", $filed_arr);
	exit;
}
mysql_close();
?>